
package com.borqs.music.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.os.Environment;

/**
 * Reflection utils to call Environment.isExternalStorageRemovable() when
 * possible, falling back to default value true when method isn't available.
 */
public class EnvironmentCompat {
    private static final Method sMethod = findApplyMethod();

    private static Method findApplyMethod() {
        try {
            Class<Environment> cls = Environment.class;
            return cls.getMethod("isExternalStorageRemovable");
        } catch (NoSuchMethodException unused) {
            // fall through
        }
        return null;
    }

    public static boolean isExternalStorageRemovable() {
        if (sMethod != null) {
            try {
                sMethod.invoke(null);
                return (Boolean) sMethod.invoke(null);
            } catch (InvocationTargetException unused) {
                unused.printStackTrace();
                // fall through
            } catch (IllegalAccessException unused) {
                unused.printStackTrace();
                // fall through
            }
        }

        return true;
    }
}
